<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/9/6
 * Time: 12:10 下午
 */

namespace app\common\cachemodel\live;

use app\common\cachemodel\Goods;
use app\common\cachemodel\MallGoodsAttr;
use app\common\lib\Util;
use app\common\model\BaseCache;
use think\Cache;

/**
 * Class LiveGoodsActivity
 * @package app\common\cachemodel\live
 */
class LiveGoodsActivity extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_goods_activity';
    protected $cacheModel = 'live.live_goods';

    /**
     * 获取直播间推荐商品活动list
     * @param $liveId
     * @param $type
     * @param bool $isFilterShow 是否过滤显示
     * @return array|mixed
     * @throws \Exception
     */
    public function getAnchorGoods($liveId, $type, $isFilterShow = false)
    {
        $ancho = (new LiveAnchorInfo())->getInfoById($liveId);
        $data = $this->batchGetInfoByIds($ancho['gids']);
        $res = [];
        if ($type)
        {
            foreach ($data as $datum)
            {
                if ($isFilterShow)
                {
                    $isShow = $datum['is_show_live'] ?? 1;
                    if ($datum['category'] == $type && $datum['status'] == 1 && $isShow)
                    {
                        $res[] = $datum;
                    }
                } else
                {
                    if ($datum['category'] == $type && $datum['status'] == 1)
                    {
                        $res[] = $datum;
                    }
                }
            }
        } else
        {
            $res = $data;
        }
        return $res;
    }

    /**
     * 重组首页数据
     * @param $data
     * @param int $liveId 直播间ID
     * @param int $liveRecordId
     * @param int $type 数据类型 1=>直播用户端数据 2=>直播主播端数据
     * @return array
     * @throws \Exception
     */
    public function resetGoods($data, int $liveId, int $liveRecordId, int $type = 1)
    {
        $arr = [];
        $recommend_gid = '';
        if ($type === 2)
        {
            $recommend_gid = Cache::get('live_recommend_goods_' . $liveRecordId, '');
        }
        $mallGoodsAttrModel = new MallGoodsAttr();
        foreach ($data as $datum)
        {
            $orPrice = '';
            $nowPrice = '';
            $isHave = false;
            if ($datum['status'] == 1)
            {
                $isHave = true;
            }

            if ($isHave === true)
            {
                $explode = explode(',', $datum['gids']);
                sort($explode);
                $gid = current($explode);
                $show_type = 1;//展示形式 1=>一排两个 2=>一排一个
                if ($datum['category'] == 2)
                {
                    $show_type = 2;
                    $tmp = $mallGoodsAttrModel->batGetInfo([$gid], ['now_price']);
                    $orPrice = sprintf('%.2f', current($tmp)['now_price']);
                    $nowPrice = sprintf('%.2f', $datum['attr']);
                }
                (new Goods())->goodsTargetLink((int)$datum['gtype'], $datum['link']);
                if ($type === 1)
                {
                    if ($datum['category'] == 1)
                    {
                        if ($datum['attr'] != 1)
                        {
                            $discount = (new Util())->numberChinese($datum['attr'] * 10);
                            $btnTxt = "特惠{$discount}折";
                        } else
                        {
                            $btnTxt = '立即购买';
                        }
                    } else
                    {
                        $btnTxt = '立即购买';
                    }

                    $arr[] = [
                        'img' => $datum['img'],
                        'title' => $datum['name'],
                        'desc' => $datum['des'],
                        'btn_txt' => $btnTxt,
                        'or_price' => $orPrice ?: 0,
                        'now_price' => $nowPrice ?: 0,
                        'target' => $datum['target'],
                        'link' => $datum['link'] . '&live_id=' . $liveId . "&live_record_id={$liveRecordId}&is_jump_intro=1",
                        'show_type' => $show_type
                    ];
                } else
                {
                    $is_recommend = ($datum['category'] . '-' . $datum['id']) === $recommend_gid;

                    $arr[] = [
                        'img' => $datum['img'],
                        'title' => $datum['name'],
                        'desc' => $datum['des'],
                        'btn_txt' => $is_recommend ? '关闭推荐' : '推荐',
                        'or_price' => $orPrice ?: 0,
                        'now_price' => $nowPrice ?: 0,
                        'show_type' => $show_type,
                        'is_recommend' => $is_recommend,
                        'id' => (int)$datum['id']
                    ];
                }
            }
        }
        return $arr;
    }

    /**
     * 批量获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $where = ['id' => ['in', $ids]];
            } else
            {
                if ($type[1])
                {
                    $where = ['status' => 1, 'category' => $type[1]];
                } else
                {
                    $where = ['status' => 1];
                }
            }
            $data = $this
                ->where($where)
                ->order('sort DESC, id DESC')
                ->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 获取直播间商品对应的推荐活动list
     * @param $liveId
     * @param $goodsType
     * @param $goods
     * @param array $param [isCloseActive=>是否关闭活动信息]
     * @throws \Exception
     */
    public function getLiveOrderTypeGoods($liveId, $goodsType, &$goods, $param = [])
    {
        $type = $goodsType == 14 ? 2 : 1;
        $actives = $this->getAnchorGoods($liveId, $type);
        $gids = array_column($goods, 'id');
        //获取当前商品所有开启的活动信息
        foreach ($actives as $active)
        {
            //获取参与活动的gid
            $acGids = $active['gids'] ? explode(',', $active['gids']) : [];

            $actingGids = array_intersect($acGids, $gids);//当前参与活动的gid

            if ($actingGids)
            {
                foreach ($actingGids as $gid)
                {
                    if (isset($param['isCloseActive']) && $param['isCloseActive'] === true && (int)$active['type'] === 1 && $active['attr'] == 1)
                    {
                        continue;
                    }
                    if ($goodsType == 14)
                    {
                        $goods[$gid]['now_prize'] = $goods[$gid]['now_price'];
                    }
                    if ((int)$active['type'] === 2)
                    {
                        $price = $active['attr'];
                    } else
                    {
                        $price = $goods[$gid]['now_prize'] * $active['attr'];
                    }
                    $goods[$gid]['live_active'] = [
                        'id' => $active['id'],
                        'name' => $active['name'],
                        'des' => $active['des'],
                        'type' => $active['type'],
                        'attr' => $active['attr'],
                        'price' => $price,//优惠后的最终价格
                        'discount_price' => $goods[$gid]['now_prize'] - $price,//优惠金额
                    ];
                }
            }
            $gids = array_diff($gids, $actingGids);//剩余未参与当前活动的gid
        }
    }
}
